﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib.Interface;
using Cspl.AnimalFight.Lib.EventHandlers;

namespace Cspl.AnimalFight.Lib.Data
{
    public class Dog : AnimalBase, IAttack, IDefend, IAttackable
    {
        private DogMood _mood;

        public event AttackDelegate AttackEvent;
        public event DefendDelegate DefendEvent;

        public bool IsDefending
        {
            get;
            set;
        }

        public DogMood Mood
        {
            get { return _mood; }
            set { _mood = value; }
        }
        
        public void Attack(string targetName)
        {
            int theRealAttcakPower = 0;

            if (this.Mood == DogMood.Bad)
            {
                theRealAttcakPower = (int)(0.5 * this.AP);
            }
            else
            {
                theRealAttcakPower = this.AP;
            }

            if (AttackEvent != null)
            {
                AttackEventHandlers eventHandler = new AttackEventHandlers
                {
                    AnimalTargetName = targetName,
                    AP = theRealAttcakPower
                };

                AttackEvent(this, eventHandler);
            }

        }

        public void Defend()
        {
            if(DefendEvent != null)
            {
                DefendEvent(this, new DefendEventHandlers
                {
                    AnimalName = this.Name,
                    Target = Animals.Dog
                });
            }
        }

        public void Attacked(int ap)
        {
            if (this._hasEscaped)
            {
                Console.WriteLine("{0} has escaped. Cannot be attacked", this.Name);
            }
            else if (this._isDead)
            {
                Console.WriteLine("{0} is already dead. Cannot be attacked", this.Name);
            }
            else
            {
                Console.WriteLine("{0} is being attacked. Current HP={1}", this.Name, this.HP);
                int realDP = this.IsDefending ? this.DP + 10 : this.DP;
                realDP = this.Mood == DogMood.Bad ? realDP - 10 : realDP;

                // if the attack is less powerfull than defense then it will be absorbed
                this.HP -= (ap - realDP);

                Console.WriteLine("HP is decreased -{0}", ap - realDP);
                Console.WriteLine("Current HP for {0} = {1}", this.Name, this.HP);

                if (this.HP <= 0)
                {
                    Console.WriteLine("The {0} is dead.", this.Name);
                    this.IsDead = true;
                }
            }
        }
    }

    public enum DogMood { Good, Bad }
}
